package com.ljh.onlinetest.controller.student;

import com.github.pagehelper.PageInfo;
import com.ljh.onlinetest.base.BaseApiController;
import com.ljh.onlinetest.base.RestResponse;
import com.ljh.onlinetest.domain.ExamPaper;
import com.ljh.onlinetest.service.ExamPaperAnswerService;
import com.ljh.onlinetest.service.ExamPaperService;
import com.ljh.onlinetest.utils.DateTimeUtil;
import com.ljh.onlinetest.utils.PageInfoHelper;
import com.ljh.onlinetest.viewmodel.admin.paper.PaperEditRequestVM;
import com.ljh.onlinetest.viewmodel.student.exam.ExamPaperPageResponseVM;
import com.ljh.onlinetest.viewmodel.student.exam.ExamPaperPageVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author ljh
 * @ClassName ExamPaperController.java
 * @createTime 2022年03月09日
 */
@RestController("StudentExamPaperController")
@RequestMapping("/api/student/exam/paper")
public class ExamPaperController extends BaseApiController {
    private final ExamPaperService examPaperService;
    private final ExamPaperAnswerService examPaperAnswerService;
    private final ApplicationEventPublisher eventPublisher;

    @Autowired
    public ExamPaperController(ExamPaperService examPaperService, ExamPaperAnswerService examPaperAnswerService, ApplicationEventPublisher eventPublisher) {
        this.examPaperService = examPaperService;
        this.examPaperAnswerService = examPaperAnswerService;
        this.eventPublisher = eventPublisher;
    }

    @PostMapping("/pageList")
    public RestResponse<PageInfo<ExamPaperPageResponseVM>> pageList(@RequestBody @Valid ExamPaperPageVM model){
        PageInfo<ExamPaper> pageInfo = examPaperService.studentPage(model);
        PageInfo<ExamPaperPageResponseVM> vmPageInfo = PageInfoHelper.copyMap(pageInfo, examPaper -> {
            ExamPaperPageResponseVM vm = modelMapper.map(examPaper, ExamPaperPageResponseVM.class);
            vm.setCreateTime(DateTimeUtil.dateFormat(examPaper.getCreateTime()));
            return vm;
        });
        return RestResponse.ok(vmPageInfo);
    }

    @PostMapping("/select/{id}")
    public RestResponse<PaperEditRequestVM> select(@PathVariable Integer id){
        PaperEditRequestVM vm = examPaperService.examPaperToVM(id);
        return RestResponse.ok(vm);
    }
}
